<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
</head>
<body>
  <script>
    function toData(obj){
      if(obj == null) return obj;
      var arr = [];
      for(var i in obj){
        var str = i+"="obj[i];
        arr.push(str)
      }
      return arr.join('&')
    }

    function ajax(obj){
      obj.type = obj.type || "get";
      obj.async = obj.async || true;
      obj.data = obj.data || null;
      if(window.XMLHttpRequest){
        var ajax = new XMLHttpRequest();
      }else{
        var ajax = new ActiveXObject("Microsoft.XMLHTTP");
      }

      if (obj.type == "post"){
        ajax.open(obj.type,obj.url,obj.async);
        ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        var data = toData(obj.data);
        ajax.send(data);
      }else{
          //get  test.php?xx=xx&aa=xx
          var url = obj.url+"?"+toData(obj.data);
          ajax.open(obj.type,url,obj.async);
          ajax.send();
      }

      ajax.onreadystatechange = function (){
        if (ajax.readyState == 4){
          if (ajax.status>=200&&ajax.status<300 || ajax.status==304){
            if (obj.success){
              obj.success(ajax.responseText);
            }
          }else{
            if (obj.error){
              obj.error(ajax.status);
            }
          }
        }
      }
    }
  </script>
</body>
</html>
